const axios = require('axios')
// const cheerio = require('cheerio');
// const fs = require('fs');
const { tabs } = require('./index');
// const interceptor = require('../config/interceptor')
// const wallpaper = require('wallpaper')
const service = {
    getWallpaperData({ timer = true }) {
        let datas = {}
        let tagData = {}
        let wordData = {}
        let catetoryData = {}
        // 分类
        return axios.get('https://app.huabao.360.cn/win/v4/label/reco?m2=338fde987bb30aabba5253aea35730feab5041192b0a&mid=00dc2d94fe9ef3ecfca4271060dd5582&uid=2efee30e9c182e676652963d3dd7e418&v=4.0.25.0&m=00dc2d94fe9ef3ecfca4271060dd5582&src=recommend&c=1').then(catetory => {
            // console.log(catetoryData, 'wallpaper');
            catetoryData.imgList = catetory.data.data.img_list
            catetoryData.list = catetory.data.data.list
            // 关键词
            return axios.get('https://app.huabao.360.cn/conf/web?m2=338fde987bb30aabba5253aea35730feab5041192b0a&mid=00dc2d94fe9ef3ecfca4271060dd5582&uid=2efee30e9c182e676652963d3dd7e418&v=4.0.25.0&m=00dc2d94fe9ef3ecfca4271060dd5582').then(word => {
                wordData.hotWordList = word.data.data.hot_word
                wordData.recWordList = word.data.data.rec_word
                // 标签
                return axios.get('https://app.huabao.360.cn/win/v4/reco/official-tag?m2=338fde987bb30aabba5253aea35730feab5041192b0a&mid=00dc2d94fe9ef3ecfca4271060dd5582&uid=2efee30e9c182e676652963d3dd7e418&v=4.0.25.0&m=00dc2d94fe9ef3ecfca4271060dd5582&lid=0&od=1&msize=0&vip=0&kind=0&oft=1&size=15&page=2&c=1&hsid=8508221782').then(word => {
                    tagData = word.data.data
                    // 列表数据
                    if (timer) { // 定时任务写入文件
                        let ids = [...catetoryData.imgList.map(el => el.id), ...catetoryData.list.map(el => el.id)]
                        let id = ids[Math.floor(Math.random() * (ids.length - 1))]
                        axios.get(`https://app.huabao.360.cn/win/v4/label/recm?m2=338fde987bb30aabba5253aea35730feab5041192b0a&mid=00dc2d94fe9ef3ecfca4271060dd5582&uid=2efee30e9c182e676652963d3dd7e418&v=4.0.25.0&m=00dc2d94fe9ef3ecfca4271060dd5582&lid=${id}&od=1&msize=0&vip=0&kind=0&oft=&size=100&page=1&c=1&hsid=8508221782`).then(result => {
                            datas = result.data.data
                        })
                        return
                    }
                    return { catetoryData, wordData, tagData, datas }
                })
            })
        })
    },
    getList (ctx) {
        return service.getWallpaperData({ timer: false }).then(({ catetoryData, wordData, tagData, datas }) => {
            return service.render({ catetoryData, wordData, tagData, datas, tabs, ctx })
        })
    },
    render ({ catetoryData, wordData, tagData, datas, ctx, tabs }) {
        let ids = [...catetoryData.imgList.map(el => el.id), ...catetoryData.list.map(el => el.id)]
        let id = Number(ctx.query.id) || ids[Math.floor(Math.random() * (ids.length - 1))]
        ctx.query.id = id
        return axios.get(`https://app.huabao.360.cn/win/v4/label/recm?m2=338fde987bb30aabba5253aea35730feab5041192b0a&mid=00dc2d94fe9ef3ecfca4271060dd5582&uid=2efee30e9c182e676652963d3dd7e418&v=4.0.25.0&m=00dc2d94fe9ef3ecfca4271060dd5582&lid=${id}&od=1&msize=0&vip=0&kind=0&oft=&size=100&page=1&c=1&hsid=8508221782`).then(result => {
            datas = result.data.data
            datas.list = datas.list ? datas.list.filter(el => el.img) : []
            let current = catetoryData.imgList.filter(el => el.id == id)[0]
            current = current || catetoryData.list.filter(el => el.id == id)[0] || {}
            // console.log(ctx, 'current');
            return { pageTitle: "精选壁纸-"+(current.name||''), catetoryData, wordData, tagData, datas, query: { ...ctx.query, id: ctx.query.id || current.id }, tabs }
            // res.send({ pageTitle: "精选壁纸-"+current, catetoryData, wordData, tagData, datas, query: req.query, tabs })
        })
    }
}
module.exports = service